package com.sinopec.handdevice.view

import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.widget.TextView
import androidx.annotation.DrawableRes
import com.alibaba.android.arouter.facade.annotation.Route
import com.gyf.immersionbar.ImmersionBar
import com.jakewharton.rxbinding3.view.clicks
import com.sinopec.basemodule.adapter.CommonAdapter
import com.sinopec.basemodule.adapter.CustomViewHolder
import com.sinopec.basemodule.base.BaseActivity
import com.sinopec.basemodule.base.BasePresenter
import com.sinopec.basemodule.base.CustomToolbar
import com.sinopec.basemodule.dialog.CommonTipsPopupWindow
import com.sinopec.basemodule.launchcontract.ErrorPalletCodesResultLaunch
import com.sinopec.basemodule.model.ErrorPalletCodeResultModel
import com.sinopec.basemodule.utils.ARouterManagerUtils
import com.sinopec.handdevice.R
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.activity_error_code_list.*
import kotlinx.android.synthetic.main.item_error_code.*
import java.util.concurrent.TimeUnit

/**
 * @author lwj
 * @ClassName: ErrorCodeListActivity
 * @Description:错误托盘校验结果界面
 */
@Route(path = ARouterManagerUtils.GOTO_MAIN_ERROR_CODE_LIST_ACTIVITY)
class ErrorCodeListActivity : BaseActivity<BasePresenter>() {

    private var datas: ArrayList<ErrorPalletCodeResultModel> = arrayListOf()
    private var adapter: CommonAdapter<ErrorPalletCodeResultModel>? = null

    private val mRequest by lazy {
        ErrorPalletCodesResultLaunch.getParams(this)
    }

    //提示语弹窗
    private lateinit var commonTipsPopupWindow: CommonTipsPopupWindow

    override fun getContentView(): Int {
        return R.layout.activity_error_code_list
    }

    override fun initView(savedInstanceState: Bundle?) {
        //总编码数
        tv_totalCount.text = mRequest?.allPalletCodes?.size.toString()
        //正常编码数
        val normalCount = mRequest?.allPalletCodes?.size?.minus(mRequest?.errorPalletCodes?.size ?: 0)
        tv_normalCount.text = normalCount.toString()
        //异常编码数
        tv_errorCount.text = mRequest?.errorPalletCodes?.size.toString()

        datas.clear()
        datas.addAll(mRequest?.errorPalletCodes ?: ArrayList())
        adapter = object :
            CommonAdapter<ErrorPalletCodeResultModel>(mContext, R.layout.item_error_code, datas) {
            override fun convert(
                holder: CustomViewHolder,
                model: ErrorPalletCodeResultModel,
                position: Int,
            ) {
                val itemView = holder.itemView
                //托盘编码
                val tv_palletCode = holder.getView<TextView>(R.id.tv_palletCode)
                tv_palletCode.text = model.palletCode
                //错误原因
                val tv_palletMessage = holder.getView<TextView>(R.id.tv_palletMessage)
                tv_palletMessage.text = model.palletMessage
                if (position % 2 == 0) {
                    itemView.setBackgroundColor(Color.parseColor("#ffffff"))
                } else {
                    itemView.setBackgroundColor(Color.parseColor("#F5F8FA"))
                }
            }
        }
        rv_list.adapter = adapter
    }

    override fun setStatusBar() {
        ImmersionBar.with(this).statusBarDarkFont(true).init()
    }


    override fun initEvent() {
        super.initEvent()

        /**
         * 确认
         */
        addDisposable(ll_scan
            .clicks()
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                val html =
                    "本次共扫码 <font color='#ED7254'>${mRequest?.allPalletCodes?.size}</font> 块，异常编码 <font color='#ED7254'>${mRequest?.errorPalletCodes?.size}</font> 块，是否确认重新扫码？重新扫码将自动为您删除异常编码，保留正常编码。"
                showCommonTipsPopupWindow(0, "温馨提示", html, "确认", "取消")
            })
    }

    override fun configToolbar(rToolbar: CustomToolbar) {
        //配置标题
        rToolbar.setTitle("异常编码")
    }

    /**
     * @author lwj
     * @Description: 二次确认通用弹窗
     */
    private fun showCommonTipsPopupWindow(
        @DrawableRes resId: Int,
        title: String,
        content: String,
        confirmText: String,
        cancelText: String,
    ) {
        commonTipsPopupWindow = CommonTipsPopupWindow.create(
            mContext,
            resId,
            title,
            content,
            confirmText,
            cancelText,
            object : CommonTipsPopupWindow.OnSelectListener {
                override fun onConfirm() {
                    ErrorPalletCodesResultLaunch.setResult(this@ErrorCodeListActivity.mContext,
                        true)
                    finish()
                }

                override fun onCancel() {
                    commonTipsPopupWindow.dismiss()
                }
            }).setAnchorView(mContext.rootView)
            .apply()
        commonTipsPopupWindow.showAtLocation(mContext.rootView, Gravity.CENTER, 0, 0)
    }
}